package com.dartfar.base.service;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.type.IntegerType;
import org.hibernate.type.StringType;
import org.hibernate.type.Type;
import org.springframework.stereotype.Component;

import com.dartfar.base.dao.DaoUtil;
import com.dartfar.base.model.T_User;

@Component("userService")
public class UserServiceImpl extends BaseServiceImpl<T_User> implements UserService {

	@Resource
	private DaoUtil<T_User> userDaoUtil;
	
	
	/**
	 * 判断某部门中是否有用户
	 * @param 部门对象
	 * @return 返回 部门里是否存在用户
	 */
	@Override
	public boolean existUserByDepartId(int id){
		long count = userDaoUtil.getCount("from T_User u where u.person.depart.id = ?", new Object[]{id},new Type[]{new IntegerType()});
		if(count == 0){
			return false;
		}else{
			return true;
		}
	}


	/**
	 * 获取用户列表
	 * @return 用户列表
	 */
	@Override
	public List<T_User> getList() {
		return userDaoUtil.getList("from T_User");
	}


	/**
	 * 根据登陆名获取用户对象
	 * @return 用户对象
	 */
	@Override
	public T_User getByName(String name) {
		List<T_User> list = userDaoUtil.getList("from T_User u where u.loginName=?",new Object[]{name},new Type[]{new StringType()});
		if(list.size()>0){
			return list.get(0);
		}else{
			return null;
		}
	}
	
	/**
	 * 获取用户
	 * @param id 用户ID
	 * @return 用户对象
	 */
	@Override
	public T_User get(int id) {
		return userDaoUtil.get(id, T_User.class);
	}
	
	
	/**
	 * 根据部门ID获取用户的分页信息
	 * @param departId 部门ID
	 * @param pageNo 页数
	 * @param pageSize 页面条数
	 * @return
	 */
	@Override
	public List<T_User> getListForPageByDepartId(int departId, int pageNo, int pageSize) {
		System.out.println("departId: " + departId);
		if(departId == 0 || departId == 1){
			return userDaoUtil.getListForPage("from T_User u order by u.id desc", (pageNo-1)*pageSize, pageSize);
			
		}else{
			return userDaoUtil.getListForPage("from T_User u where u.person.depart.id = ? order by u.id desc", new Object[]{departId}, new Type[]{new IntegerType()}, (pageNo-1)*pageSize, pageSize);			
		}
	}
	
	
	/**
	 * 根据部门ID获取用户的数量
	 * @param curDepartId 部门ID
	 * @return 用户的数量
	 */
	@Override
	public Long getCountByDepartId(int curDepartId) {
		if(curDepartId == 0 || curDepartId == 1){
			return userDaoUtil.getCount("from T_User");
		}else{
			return userDaoUtil.getCount("from T_User u where u.person.depart.id=?", new Object[]{curDepartId}, new Type[]{new IntegerType()});
		}
			
	}
	
}
